<?php
/**
 * 漫友体验
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class ComicfansExperience extends Model
{
    //set talbename
    protected $table = 'comicfans_experience';
    //set primary key
    protected $pk = 'id';


    /**
     * @desc 增加漫友体验
     * @return false|int
     */
    public function addComicfansExperience($param)
    {
        $param['create_time'] = time();
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    /**
     * @desc 根据条件获取漫友体验
     * @return false|int
     */
    public function getComicfansExperienceByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return $this->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    public function updateComicfansExperience($id, $param)
    {
        $conds = array(
            'id' => $id,
        );
        $field = array();

        if (isset($param['sort'])) {
            $field['sort'] = $param['sort'];
        }

        if (isset($param['note_id'])) {
            $field['note_id'] = $param['note_id'];
        }

        if (isset($param['status'])) {
            $field['status'] = $param['status'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function getCount($conds = [], $filed = 'id')
    {
        $this->allowField(true);
        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($arrCond)->count($filed);
    }

    /**
     * @desc 删除购物车
     * @param $cart_id
     * @return int
     */
    public function deleteComicfansExperience($id)
    {
        $conds = array(
            'id' => $id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * @return \think\paginator\Collection
     */
    public function CommicExperiencePage()
    {
        return $this->where(['status' => '0'])->paginate(10);
    }

}
